//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////@

Skiniko.prototype.stisimo = function() {
	Log.print('reconstructing scenery');
	Log.level.push();

	this.sitkep = {};
	this.stisimoProsklisiCleanup(DB.connection());
	return this;
}

Skiniko.prototype.stisimoProsklisiCleanup = function(conn) {
	var query, skiniko = this;

	Log.print('deleting orphan "prosklisi" rows from the database');
	query = 'DELETE FROM `prosklisi` WHERE `trapezi` NOT IN ' +
		'(SELECT `kodikos` FROM `trapezi` WHERE `arxio` IS NULL)';
	conn.query(query, function(conn) {
		if (conn.affectedRows) console.log(conn.affectedRows + ' rows deleted');
		skiniko.stisimoSizitisiCleanup(conn);
	});
}

Skiniko.prototype.stisimoSizitisiCleanup = function(conn) {
	var query, skiniko = this;

	Log.print('deleting orphan "sizitisi" rows from the database');
	query = 'DELETE FROM `sizitisi` WHERE (`trapezi` IS NOT NULL) AND (`trapezi` NOT IN ' +
		'(SELECT `kodikos` FROM `trapezi` WHERE `arxio` IS NULL))';
	conn.query(query, function(conn) {
		if (conn.affectedRows) console.log(conn.affectedRows + ' rows deleted');
		skiniko.stisimoTrapezi(conn);
	});
}

Skiniko.prototype.stisimoTrapezi = function(conn) {
	var query, skiniko = this;

	Log.print('reading "trapezi" rows from the database');
	query = 'SELECT `kodikos`, UNIX_TIMESTAMP(`stisimo`) AS `stisimo`, `pektis1`, ' +
		'`apodoxi1`, `pektis2`, `apodoxi2`, `poll` FROM `trapezi` WHERE `arxio` IS NULL';
	conn.query(query, function(conn, rows) {
		var i;
		for (i = 0; i < rows.length; i++) {
			skiniko.trapeziAdd(new Trapezi().trapeziRowSet(rows[i]));
			if (rows[i].pektis1) skiniko.sitkep[rows[i].pektis1] = {};
			if (rows[i].pektis2) skiniko.sitkep[rows[i].pektis2] = {};
		}

		skiniko.stisimoTrparam(conn);
	});
}

Skiniko.prototype.stisimoTrparam = function(conn) {
	var query, skiniko = this;

	Log.print('reading "trparam" rows from the database');
	query = 'SELECT `trapezi`, `param`, `timi` FROM `trparam` WHERE `trapezi` IN ' +
		'(SELECT `kodikos` FROM `trapezi` WHERE `arxio` IS NULL)';
	conn.query(query, function(conn, rows) {
		var i, trapezi;
		for (i = 0; i < rows.length; i++) {
			trapezi = skiniko.trapeziGet(rows[i].trapezi);
			if (!trapezi) continue;

			trapezi.trparamSet(new Trparam().trparamRowSet(rows[i]));
		}

		skiniko.stisimoProsklisi(conn);
	});
}

Skiniko.prototype.stisimoProsklisi = function(conn) {
	var query, skiniko = this;

	Log.print('reading "prosklisi" rows from the database');
	query = 'SELECT `kodikos`, `trapezi`, `apo`, `pros`, UNIX_TIMESTAMP(`epidosi`) ' +
		'AS `epidosi` FROM `prosklisi` ORDER BY `kodikos`';
	conn.query(query, function(conn, rows) {
		var i, trapezi;
		for (i = 0; i < rows.length; i++) {
			trapezi = skiniko.trapeziGet(rows[i].trapezi);
			if (!trapezi) continue;

			trapezi.prosklisiAdd(new Prosklisi().prosklisiRowSet(rows[i]));
			skiniko.sitkep[rows[i].apo] = {};
			skiniko.sitkep[rows[i].pros] = {};
		}

		skiniko.stisimoPartida(conn);
	});
}

Skiniko.prototype.stisimoPartida = function(conn) {
	var query, telpar = {}, skiniko = this;

	Log.print('reading "partida" rows from the database');
	query = 'SELECT `kodikos`, `trapezi`, UNIX_TIMESTAMP(`enarxi`) AS `enarxi`, ' +
		'`protos`, `nikitis`, UNIX_TIMESTAMP(`telos`) AS `telos` FROM `partida` ' +
		'WHERE `trapezi` IN (SELECT `kodikos` FROM `trapezi` WHERE `arxio` IS NULL) ' +
		'ORDER BY `kodikos`';
	conn.query(query, function(conn, rows) {
		var i, trapezi;
		for (i = 0; i < rows.length; i++) {
			trapezi = skiniko.trapeziGet(rows[i].trapezi);
			if (!trapezi) continue;

			trapezi.partidaAdd(telpar[trapezi.kodikos] = new Partida().partidaRowSet(rows[i]));
		}

		Log.print('reading "energia" rows from the database');
		skiniko.stisimoEnergia(conn, telpar);
	});
}

Skiniko.prototype.stisimoEnergia = function(conn, telpar) {
	var trapezi, partida, query, skiniko = this;

	for (trapezi in telpar) {
		partida = telpar[trapezi];
		delete telpar[trapezi];
		query = 'SELECT `kodikos`, `partida`, `pektis`, `idos`, `data`, ' +
			'UNIX_TIMESTAMP(`pote`) AS `pote` FROM `energia` WHERE `partida` = ' +
			partida.kodikosGet() + ' ORDER BY `kodikos`';

		conn.query(query, function(conn, rows) {
			var i;
			for (i = 0; i < rows.length; i++) {
				partida.energiaAdd(new Energia().energiaRowSet(rows[i]));
			}

			skiniko.stisimoEnergia(conn, telpar);
		});

		return;
	}

	this.stisimoSizitisi(conn);
}

Skiniko.prototype.stisimoSizitisi = function(conn) {
	var query, skiniko = this;

	Log.print('reading "sizitisi" rows from the database');
	query = 'SELECT `kodikos`, `trapezi`, `pektis`, `sxolio`, UNIX_TIMESTAMP(`pote`) AS `pote` ' +
		'FROM `sizitisi` ORDER BY `kodikos` DESC LIMIT ' + geister.sxolioMin;
	conn.query(query, function(conn, rows) {
		var i, sizitisi, trapezi;
		for (i = rows.length - 1; i >= 0; i--) {
			sizitisi = new Sizitisi().sizitisiRowSet(rows[i]);
			trapezi = sizitisi.trapeziGet();
			if (!trapezi) {
				skiniko.sizitisiAdd(sizitisi);
				continue;
			}

			trapezi = skiniko.trapeziGet(trapezi);
			if (!trapezi) continue;

			trapezi.sizitisiAdd(sizitisi);
		}

		Log.print('reading "pektis" rows from the database');
		skiniko.stisimoSinedria(conn);
	});
}

Skiniko.prototype.stisimoSinedria = function(conn) {
	var query, skiniko = this;

	Log.print('reading "sinedria" rows from the database');
	query = 'SELECT `pektis`, `klidi`, `ip`, UNIX_TIMESTAMP(`isodos`) AS `isodos`, ' +
		'`trapezi`, `thesi`, `simetoxi` FROM `sinedria`';
	conn.query(query, function(conn, rows) {
		var i;
		conn.free();
		for (i = 0; i < rows.length; i++) {
			skiniko.sinedriaAdd(new Sinedria().sinedriaRowSet(rows[i]));
			skiniko.sitkep[rows[i].pektis] = {};
		}

		Log.print('reading "pektis" rows from the database');
		skiniko.stisimoPektis();
	});
}

Skiniko.prototype.stisimoPektis = function() {
	var login, skiniko = this;

	for (login in this.sitkep) {
		delete this.sitkep[login];
		this.pektisAdd(new Pektis({
			login: login,
		}).pektisLoad(function() {
			skiniko.stisimoPektis();
		}));

		return;
	}

	// Διαγράφουμε τη λίστα εμπλεκομένων παικτών, η οποία έτσι κι αλλιώς έχει
	// αδειάσει, ελέγχουμε τα στοιχεία θέσης όλων των συνεδριών και δημιουργούμε
	// τη λίστα των περιφερομένων.

	delete this.sitkep;
	this.thesiCheck();

	Log.print('Constructing "tablo" properties');
	this.stisimoTablo();
}

Skiniko.prototype.stisimoTablo = function() {
	this.trapeziWalk(function(kodikos, trapezi) {
		trapezi.tabloGet().replay();
	});

	// Το σκηνικό έχει στηθεί και ελεγχθεί όσον αφορά την πληρότητά του, επομένως
	// προχωρούμε στην τελευταία φάση που αφορά στην (επαν)ενεργοποίηση του skiser.

	this.stisimoTelos();
}

// Όταν τελειώσουν οι διαδικασίες υποδομής και έχει στηθεί το σκηνικό τού server, ενεργοποιούμε
// τις πόρτες υποδοχής αιτημάτων (normal και monitor) και βάζουμε μπρος τις διαδικασίες περιπόλου.
// Η μέθοδος "stisimoTelos" είναι το τελευταίο βήμα κατά το στήσιμο του Node server.

Skiniko.prototype.stisimoTelos = function() {
	this.kinisiReset();
	Log.level.pop();
	peripolos.startup(this);
	server.atexit(this);

	Log.print('establishing listeners');
	Log.level.push();
	server.oriste(this);
	if (skimon) skimon.oriste(this);
	Log.level.pop();
	Log.fasi.nea('Node server is up and running!');
}
